package com.prs.crm.action.sys;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.sys.BottomUrlCollection;
import com.prs.crm.service.sys.BottomUrlCollectionService;

public class BottomUrlCollectionAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private BottomUrlCollection	bottomUrlCollection;
	private BottomUrlCollectionService bottomUrlCollectionService;
	private Integer[] ids;
	private PaginationSupport pagination;
	private List<BottomUrlCollection> bottom;

	public List<BottomUrlCollection> getBottom() {
		return bottom;
	}
	public void setBottom(List<BottomUrlCollection> bottom) {
		this.bottom = bottom;
	}
	public BottomUrlCollection getBottomUrlCollection() {
		return bottomUrlCollection;
	}
	public void setBottomUrlCollection(BottomUrlCollection bottomUrlCollection) {
		this.bottomUrlCollection = bottomUrlCollection;
	}
	public BottomUrlCollectionService getBottomUrlCollectionService() {
		return bottomUrlCollectionService;
	}
	public void setBottomUrlCollectionService(
			BottomUrlCollectionService bottomUrlCollectionService) {
		this.bottomUrlCollectionService = bottomUrlCollectionService;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	@Action("listBottomUrlCollection")
	public String listBottomUrlCollection(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(BottomUrlCollection.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getBottomUrlCollectionService().listByPage(criteria, pagination));
		return SUCCESS;
	} 
	
	@Action("editBottomUrlCollection")
	public String editBottomUrlCollection(){
		if(ids!=null){
			this.bottomUrlCollection=this.getBottomUrlCollectionService().get(ids[0]);
		}
		if(bottomUrlCollection!=null){
		if(bottomUrlCollection.getCount()>4){
			addActionError("底部导航最多只能四条");
			return ERROR;
		}
	}
		return SUCCESS;
	}
	@Action(value="saveBottomUrlCollection", results={
			@Result(name="success",type="redirect",location="listBottomUrlCollection"),
			@Result(name="error",type="chain",location="editBottomUrlCollection")
	})
	public String saveBottomUrlCollection(){
		this.bottom=this.getBottomUrlCollectionService().getAllBottomUrlCollection();
		if(bottomUrlCollection.getCount()==0){
			bottomUrlCollection.setCount(1);
		}else{
			bottomUrlCollection.setCount(bottomUrlCollection.getCount()+1);
		}
		
		this.getBottomUrlCollectionService().save(bottomUrlCollection);
		if(bottomUrlCollection.getName()==null||"".equals(bottomUrlCollection.getName().trim())){
			bottomUrlCollection.setName(bottomUrlCollection.getUrl());
		}
		this.getBottomUrlCollectionService().submit(bottomUrlCollection);
		return SUCCESS;
	}
	@Action(value="saveBottomUrlCollections", results={
			@Result(name="success",type="redirect",location="editBottomUrlCollection"),
			@Result(name="error",type="chain",location="editBottomUrlCollection")
	})
	public String saveBottomUrlCollections(){
		bottomUrlCollection.setCount(bottomUrlCollection.getCount()+1);
		this.getBottomUrlCollectionService().save(bottomUrlCollection);
		if(bottomUrlCollection.getName()==null||"".equals(bottomUrlCollection.getName().trim())){
			bottomUrlCollection.setName(bottomUrlCollection.getUrl());
		}
		this.getBottomUrlCollectionService().submit(bottomUrlCollection);
		return SUCCESS;
	}
	@Action(value="removeBottomUrlCollection", results={
			@Result(name="success",type="redirect",location="listBottomUrlCollection"),
			@Result(name="error",type="chain",location="listBottomUrlCollection")
	})
	public String removeBottomUrlCollection(){
		if(ids!=null){
			this.bottomUrlCollection=this.getBottomUrlCollectionService().get(ids[0]);
		}
		this.getBottomUrlCollectionService().batchRemove(ids);
//		bottomUrlCollection.setCount(bottomUrlCollection.getCount()-1);
//		this.getBottomUrlCollectionService().submit(bottomUrlCollection);
		return SUCCESS;
	}
}
